import axios from 'axios'
import { Notify,Dialog  } from 'vant'

export function request(config) {
  // 1.创建axios的实例
  const instance = axios.create({
    // baseURL: process.env.NODE_ENV === 'development' ?
    //   'https://api1-yxtest.readboy.com' :
    //   'https://api1-yx.readboy.com',
    baseURL: 'http://127.0.0.1:3007',
    timeout: 5000
  })
  //2.响应拦截
  instance.interceptors.response.use( res=> {
    const {status} = res.data;
    const {message} = res.data;
    if(typeof status !== 'undefined') {
      if(status === 0) {
        return Promise.resolve(res.data)
      } else if(status === 1 && message ==="身份认证失败！") {
        //Notify({ type: 'danger', message: '请求失败，请检查填写信息是否有误' });
        Dialog({ message:'token过期，请重新登录！' });
        return Promise.reject(new Error(message));
      } else {
        Dialog({ message });
        return Promise.reject(new Error(message));
      }
    }
  },err => {
    console.log(err)
  })
  // 3.发送真正的网络请求
  return instance(config)
}
